package com.onlyoffice.demo.findproblem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.onlyoffice.demo.findproblem.domain.ErrorSpot;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ErrorSpotMapper extends BaseMapper<ErrorSpot> {

    /**
     * 根据关卡ID查询错误点
     */
    @Select("SELECT * FROM error_spot WHERE level_id = #{levelId}")
    List<ErrorSpot> selectByLevelId(@Param("levelId") Long levelId);

    /**
     * 批量插入错误点
     */
    @Insert("<script>" +
            "INSERT INTO error_spot (level_id, x_coordinate, y_coordinate, radius, description) VALUES " +
            "<foreach collection='spots' item='spot' separator=','>" +
            "(#{spot.levelId}, #{spot.xCoordinate}, #{spot.yCoordinate}, #{spot.radius}, #{spot.description})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("spots") List<ErrorSpot> spots);

    /**
     * 根据坐标范围查询错误点（用于点击检测优化）
     */
    @Select("SELECT * FROM error_spot WHERE level_id = #{levelId}" +
            "AND x_coordinate BETWEEN #{minX} AND #{maxX} " +
            "AND y_coordinate BETWEEN #{minY} AND #{maxY}")
    List<ErrorSpot> selectByCoordinateRange(@Param("levelId") Long levelId,
                                            @Param("minX") Float minX, @Param("maxX") Float maxX,
                                            @Param("minY") Float minY, @Param("maxY") Float maxY);
}
